<?php
/*
 *           佛曰:  
 *                   写字楼里写字间，写字间里程序员；  
 *                   程序人员写程序，又拿程序换酒钱。  
 *                   酒醒只在网上坐，酒醉还来网下眠；  
 *                   酒醉酒醒日复日，网上网下年复年。  
 *                   但愿老死电脑间，不愿鞠躬老板前；  
 *                   奔驰宝马贵者趣，公交自行程序员。  
 *                   别人笑我忒疯癫，我笑自己命太贱；  
 *                   不见满街漂亮妹，哪个归得程序员？
 * 
 * @Date         : 2020-09-01 09:42:56
 * @Author       : 真友
 * @Description  : 本代码由【真友】大力敲打
 */

declare (strict_types = 1);

namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use \app\common\Time;
class User extends \app\admin\controller\Base
{

  public function ckvo($vo = array())
  {
    //总佣金
    $vo['total_fee'] = Db::name('reward')
                ->where('to_uid',$vo['id'])
                ->sum('fee');
    //已提现
    $vo['deposit_fee'] = Db::name('deposit')
                ->where('uid',$vo['id'])
                ->where('status',1)
                ->sum('fee');
    //余额
    $vo['user_fee'] = $vo['total_fee'] - $vo['deposit_fee'];
    // //团队情况
    $team = $this->teamReport($vo['id']);
    $vo['level_1'] = $team['level_1'];
    $vo['level_2'] = $team['level_2'];
    $vo['level_3'] = $team['level_3'];
    return $vo;
    # code...
  }
  public function update()
  {
    $post = input('post.');
    $id = input('id');
    $pk = Db::name($this->con)
              ->where('id',$id)
              ->update($post);
    if ($pk > 0) {
      return $this->doSuccess('ok');
    }
    return $this->doSuccess('ok');
  }

  //用户团队人数统计
  public function teamReport($uid = 0)
  {
    //团队一级人数
    $vo = array();
    $vo['level_1'] = 0;
    $vo['level_2'] = 0;
    $vo['level_3'] = 0;

    $level_1 = getTeamUidsLevel($uid,1);
    // if ($uid == '6135107') {
    //   print_r($level_uids);
    //   return;
    // }
    $level_1 = count($level_1);
    $vo['level_1'] = $level_1;

    if ($level_1 == 0) {
      $vo['level_total'] = $level_1;
      return $vo;
      return $this->doSuccess('ok',$vo);
    }

    $level_2 = getTeamUidsLevel($uid,2);
    $level_2 = count($level_2);
    $vo['level_2'] = $level_2;
    if ($level_2 == 0) {
      $vo['level_total'] = $level_1;
      return $vo;
      return $this->doSuccess('ok',$vo);
    }

    $level_3 = getTeamUidsLevel($uid,3);
    $level_3 = count($level_3);
    $vo['level_count'] = $level_3;

    $vo['level_total'] = $level_1 + $level_2 + $level_3;
    return $vo;
    return $this->doSuccess('ok',$vo);
  }

  public function userStatus()
  {
    $post = input('post.');
    $id = input('id');
    Db::name('user')->where('id',$id)->update($post);
    return $this->doSuccess('ok');
  }

  //推荐人情况
  public function getPuser()
  {
    $uid = input('uid');
    if (!$uid) {
      return $this->doSuccess('ok',[]);
    }
    $vo = Db::name('user')->where('id',$uid)->find();
    if (!$vo) {
      return $this->doSuccess('ok',[]);
    }
    $vo = $this->ckvo($vo);
    return $this->doSuccess('ok',$vo);
  }

  //用户团队订单收益报表
  public function orderReport()
  {
    $uid = input('uid');

    if (!$uid) {
      return $this->doSuccess('ok',[]);
    }

    $vo = array();
    //订单 和 收益 情况 总量
    $query = array();
    $query[] = ['to_uid','=',$uid];
    $order_total_count = Db::name('reward')
                  ->where($query)
                  ->count('id');
    $vo['order_total_count'] = $order_total_count;

    $order_total_fee = Db::name('reward')
                  ->where($query)
                  ->sum('fee');
    $vo['order_total_fee'] = $order_total_fee;

    //订单 和 收益 情况 今日
    list($st,$et) = Time::today();
    $query = array();
    $query[] = ['pay_time','>',$st];
    $query[] = ['pay_time','<',$et];
    $query[] = ['to_uid','=',$uid];
    $order_today_count = Db::name('reward')
                  ->where($query)
                  ->count('id');
    $vo['order_today_count'] = $order_today_count;

    $order_today_fee = Db::name('reward')
                  ->where($query)
                  ->sum('fee');
    $vo['order_today_fee'] = $order_today_fee;
    //

    //订单 和 收益 情况 昨日
    list($st,$et) = Time::yesterday();
    $query = array();
    $query[] = ['to_uid','=',$uid];
    $query[] = ['pay_time','>',$st];
    $query[] = ['pay_time','<',$et];
    $order_yesterday_count = Db::name('reward')
                  ->where($query)
                  ->count('id');
    $vo['order_yesterday_count'] = $order_yesterday_count;

    $order_yesterday_fee = Db::name('reward')
                  ->where($query)
                  ->sum('fee');
    $vo['order_yesterday_fee'] = $order_yesterday_fee;

    //订单 和 收益 情况 本月
    list($st,$et) = Time::month();
    $query = array();
    $query[] = ['to_uid','=',$uid];
    $query[] = ['pay_time','>',$st];
    $query[] = ['pay_time','<',$et];
    $order_month_count = Db::name('reward')
                  // ->fetchsql(true)
                  ->where($query)
                  ->count('id');
    $vo['order_month_count'] = $order_month_count;

    $order_month_fee = Db::name('reward')
                  ->where($query)
                  ->sum('fee');
    $vo['order_month_fee'] = $order_month_fee;
    # code...

    //订单 和 收益 情况 上约
    list($st,$et) = Time::lastMonth();
    $query = array();
    $query[] = ['to_uid','=',$uid];
    $query[] = ['pay_time','>',$st];
    $query[] = ['pay_time','<',$et];
    $order_lastmonth_count = Db::name('reward')
                  ->where($query)
                  ->count('id');
    $vo['order_lastmonth_count'] = $order_lastmonth_count;

    $order_lastmonth_fee = Db::name('reward')
                  ->where($query)
                  ->sum('fee');
    $vo['order_lastmonth_fee'] = $order_lastmonth_fee;

    return $this->doSuccess('ok',$vo);
  }
  
}
